package com.buaa.utility.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	public static final String YYYY_MM_DD = "yyyy-MM-dd";

	public static final String YYYYMMDD = "yyyyMMdd";

	public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

	public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";

	public static final String YYMMDDHHMMSS = "yyMMddHHmmss";

	public static String getCurrentDate(String pattern) {
		String currDateTime = "";
		SimpleDateFormat dateFmt;
		if (pattern == null || pattern.trim().length() == 0)
			dateFmt = new SimpleDateFormat(DateUtil.YYYY_MM_DD);
		else
			dateFmt = new SimpleDateFormat(pattern);
		currDateTime = dateFmt.format(new java.util.Date());
		return currDateTime;
	}

	public static String formatDate(Date date, String pattern) throws Exception {
		SimpleDateFormat dateFmt;
		if (pattern == null || pattern.trim().length() == 0)
			dateFmt = new SimpleDateFormat(DateUtil.YYYY_MM_DD);
		else
			dateFmt = new SimpleDateFormat(pattern);
		String strDate = dateFmt.format(date);
		return strDate;
	}

	public static String getYesterDay(String pattern)
			throws Exception {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String date = dateFormat.format(new Date());
		String yesterDay = "";
		Calendar lc_calendar = Calendar.getInstance();
		String year = date.substring(0, 4);
		String month = "";
		String day = "";
		int index = date.indexOf("-");
		if (index >= 0) {
			month = date.substring(5, 7);
			day = date.substring(8, 10);
		} else {
			month = date.substring(4, 6);
			day = date.substring(6, 8);
		}
		// System.out.println(month+day);
		lc_calendar.set(Integer.parseInt(year), Integer.parseInt(month) - 1,
				Integer.parseInt(day));
		lc_calendar.add(Calendar.DAY_OF_MONTH, -1);
		Date newDate = lc_calendar.getTime();
		if (pattern == null || pattern.trim().length() == 0)
			pattern = DateUtil.YYYY_MM_DD;
		yesterDay = DateUtil.formatDate(newDate, pattern);
		return yesterDay;
	}
	
	public static int getCurrentYear(){
		Date myDate = new   Date();
		int thisYear = myDate.getYear() + 1900;
		return thisYear;// 获得当前的年
	}
	
	public static String getCurrentMonth(){
		String month = null;
		Date myDate = new  Date();
		int thisMonth = myDate.getMonth() + 1;// 获得当前的月
		if(thisMonth<10){
			month = "0"+thisMonth;
		}else{
			month = thisMonth+"";
		}
		return month;
	}
}